package org.example.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {

    private Integer code; //编码：200成功，其它数字为失败

    private String msg; //错误信息

    private T data; //数据

    private Map map = new HashMap(); //其他动态数据

    public static <T> Result<T> success(T object) {
        Result<T> r = new Result<T>();
        r.data = object;
        r.code = 200;
        return r;
    }

    public static <T> Result<T> success(Integer code, T object) {
        Result<T> r = new Result<T>();
        r.data = object;
        r.code = code;
        return r;
    }

    // 泛型方法
    public static <T> Result<T> error(String msg) {
        Result r = new Result();
        r.msg = msg;
        r.code = 400;
        return r;
    }

    public Result<T> add(String key, Object value) {
        this.map.put(key, value);
        return this;
    }
}